//
//  ITCollectionController.m
//  InTubePro
//
//  Created by seven on 2020/4/13.
//  Copyright © 2020 seven. All rights reserved.
//

#import "ITCollectionController.h"
#import "ITMJRefreshHeadGifImagesTool.h"

@interface ITCollectionController (){
}
@end

@implementation ITCollectionController

- (void)viewDidLoad {
    [super viewDidLoad];

    BaseView *topView = self.topView;

    _collectionView = [[UICollectionView alloc] initWithFrame:self.contentFrame collectionViewLayout:self.layout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [_collectionView setShowsHorizontalScrollIndicator:NO];
    [_collectionView setShowsVerticalScrollIndicator:NO];
    //[_collectionView setContentInset:UIEdgeInsetsMake(0, -5, 0, -5)];
    //[_collectionView registerClass:[ITTopicCell class] forCellWithReuseIdentifier:@"cell"];
    [self.view addSubview:_collectionView];
    
    _collectionView.backgroundColor = [UIColor clearColor];
    WEAKSELF
    _page = 1;
    _collectionView.mj_header = [ITRefreshGifHeader headerWithRefreshingBlock:^{
        STRONGSELF
        self.timestamp = 0;
        self.page = 1;
        [self loadData];
    }];
    _collectionView.mj_footer = [ITRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
    [_collectionView.mj_header beginRefreshing];
    
}

- (void)updateContentFrame{
    [super updateContentFrame];
    _collectionView.frame = self.contentFrame;
}

- (void)setOffset:(CGFloat)offset{
    _offset = offset;
    CGRect frame = self.collectionView.frame;
    frame.origin.x = 10;
    frame.size.width = self.view.width - 20;
    self.collectionView.frame = frame;
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
}

- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}

- (void)willResignActive{
    [super willResignActive];
}

- (void)didBecomeActive{
    [super didBecomeActive];
}

- (void)setupLayout{
    int width = (kScreenWidth-5)/2.0;
    int height = 165.0/185.0*width;
    _itemSize = CGSizeMake(width, height);
    _layout.itemSize = _itemSize;
    _layout.minimumLineSpacing = 5;//行
    _layout.minimumInteritemSpacing = 5;//列
    [_layout setScrollDirection:UICollectionViewScrollDirectionVertical];
}

- (UICollectionViewFlowLayout *)layout{
    if (_layout == nil) {
        _layout = [[UICollectionViewFlowLayout alloc] init];
        [self setupLayout];
    }
    return _layout;
}

- (void)onEmptyAction{
    [self.view showLoading];
    [self loadData];
}

- (void)refreshTableViewData{
    _page = 1;
    _row = 0;
    _noMoreData = NO;
    [self loadData];
}

- (void)loadData{
    _loading = YES;
    /*
    WEAKSELF
    [ITDiscoverNetworkAPIManager.shareManager getTopicsWithPage:self.page callback:^(ITNetWorkModel *ret) {
        if (ret.success) {
            [weak_self didLoadWithData:[NSArray modelArrayWithClass:ITTopicModel.class json:ret.data]];
        }else {
            [weak_self didLoadWithData:nil];
        }
    }];
     /*/
}

- (void)didLoadWithData:(NSArray *)data{
    NSMutableArray *tmp = data;
    /*if (tmp.count == 0) {
        tmp = self.randomList;
    }//*/
    
    if (_page == 1) {
        if (tmp.count == 0){
            [self showEmptyView];
        }else {
            [self hideEmptyView];
        }
        if (self.ignoreEmptyView) {
            _collectionView.mj_footer.alpha = 1;
        }
    }
    
    if (tmp == nil) {
        MainDelay(0, ^{
            if (_collectionView.mj_header.refreshing) {
                [_collectionView.mj_header endRefreshing];
            }
            if (_collectionView.mj_footer.refreshing) {
                [_collectionView.mj_footer endRefreshing];
            }
        });
        return;
    }
    if (_page == 1) {
        self.dataSource = [NSMutableArray arrayWithArray:tmp];
    }else {
//        NSMutableArray *paths = NSMutableArray.array;
//        for (int i=0; i<tmp.count; i++) {
//            [paths addObject:[NSIndexPath indexPathForRow:_dataSource.count+i inSection:0]];
//        }
        [_dataSource addObjectsFromArray:tmp];
        //[_collectionView insertItemsAtIndexPaths:paths];
    }
    [_collectionView reloadData];
    if (_page == 1) {
        [_collectionView.mj_header endRefreshing];
    }
    _collectionView.mj_footer.alpha = 1;
    _noMoreData = tmp.count < 10;
    if (tmp.count >= 10) {
        [_collectionView.mj_footer endRefreshing];
    }else{
        [_collectionView.mj_footer endRefreshingWithNoMoreData];
    }
    _page ++;
    _loading = NO;
}

#pragma -mark UICollectionViewDataSource | UICollectionViewDelegateFlowLayout
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
//    ITTopicModel *model = [self.dataSource objectAtIndex:indexPath.row];
//    cell.model = model;
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
//    ITTopicModel *model = [self.dataSource objectAtIndex:indexPath.row];
//    ITTopicController *controller = [ITTopicController new];
//    controller.topic = model;
//    [self.navigationController pushViewController:controller animated:YES];
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == _dataSource.count-1 && (indexPath.row%2)==0) {
        return CGSizeMake(collectionView.width, _itemSize.height);
    }
    return _itemSize;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGPoint offset = scrollView.contentOffset;
    
    CGFloat tmp = 0;
    if (_layout.scrollDirection == UICollectionViewScrollDirectionVertical) {
        tmp = offset.y - (scrollView.contentSize.height - scrollView.height);
    }else {
        tmp = offset.x - (scrollView.contentSize.width - scrollView.width);
    }
    if (tmp > - kScreenHeight && !_noMoreData) {//到底了 请求下一页
        if (!_loading) {
            [self loadData];
        }
    }
}

@end
